﻿/* Copyright (c) 2011-2012, Zetatron Consulting
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

* Neither the name of Zetatron Consulting nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AionCxxLibrary.Models;
using WorldServer.Services;
using AionCxxLibrary.Packets;
using AionCxxLibrary.Utilities;
using AionCxxLibrary.Enums;
using WorldServer.Packets.Server;
using WorldServer.Data;

namespace WorldServer.Models
{
    public class Character : AionObject, IDisposable
    {
        public TCharacter Template;
        public WorldConnection Connection;
        public CharacterSkillList SkillList;
        public Dictionary<StorageType, Storage> Storages;
        public QuestList Quests;
        public RecipeList Recipes;
        public FriendsList Friends;
        public BlockList Blocks;
        public byte SocialState = 0;
        public UI UI;

        ushort ChannelID;

        public bool isAdmin = true;

        public AionAction CurrentAction;

        public Storage Inventory
        {
            get
            {
                return Storages[StorageType.Cube];
            }
        }

        public bool Connected = true;

        public object Mutex_Item = new object();
        public object Mutex_UseSkill = new object();

        public Character(TCharacter template) : base(template.ID)
        {
            Template = template;
            Position = new Vector5D(template.WorldID, Template.Position);
            StatsList = new StatsList(this);
            Storages = new Dictionary<StorageType, Storage>();
            UI = new UI(this);
        }

        public void SendMessage(string msg)
        {
            Connection.SendPacket(new SPkt_SocialPublicMessage(PublicMessageType.SystemWhite, null, msg));
        }

        public void SendClientMessage(AString message, params object[] parameters)
        {
            Connection.SendPacket(new SPkt_SystemMessage(false, (int)message, 0, parameters));
        }

        public void SendPacket(AionServerPacket pkt)
        {
            Connection.SendPacket(pkt);
        }

        public void Dispose()
        {
            Log.Debug("Destroying player " + Template.Name);
            Connected = false;
            SkillList.Clear();
            StatsList.Clear();
            Storages.Clear();
            Template = null;
        }

        public bool IsEnemy(Character c)
        {
            return false;
        }

        public override void SeeObject(AionObject obj)
        {
            obj.Pkt_Info(this);
            Log.Info(Template.Name + " sees " + obj.GetGUID());
        }

        public override void UnseeObject(AionObject obj)
        {
            obj.Pkt_Delete(this);
            Log.Info(Template.Name + " doesn't see " + obj.GetGUID() + " anymore.");
        }

        /// <summary>
        /// For system use only.
        /// Use LevelInstance.ChannelID to get ID of char's channel.
        /// </summary>
        public ushort GetChannelID()
        {
            if (LevelInstance == null)
                return ChannelID;
            else
                return LevelInstance.ChannelID;
        }
        public void SetChannelID(ushort val)
        {
            ChannelID = val;
        }

        /// <summary>
        /// Find an item in one of this character's storages given its GUID.
        /// </summary>
        /// <param name="guid">Item GUID to find</param>
        /// <returns>Found item or null if none matched.</returns>
        public KeyValuePair<Storage, Item> LookupStorageItem(int guid)
        {
            foreach (Storage storage in Storages.Values)
            {
                Item res = storage.Find(i => i.GetGUID() == guid);
                if (res != null)
                    return new KeyValuePair<Storage, Item>(storage, res);
            }
            return new KeyValuePair<Storage,Item>(null, null);
        }

    }

    public class AionAction
    {
        public AionActionType Type;
        public int ArgInt;
        public object ArgObject;
    }

    public enum AionActionType
    {
        UseSkill = 0,
    }

}
